import { Application } from 'egg';
import { CoinStatus, CoinProject, CoinType } from '../constants';
import * as pageFindPlugin from 'mongoose-paginate';

export default (app: Application) => {
  const mongoose = app.mongoose;
  const ObjectId = mongoose.Schema.Types.ObjectId;
  const schema = new mongoose.Schema({
    // 用户id
    user: {
      type: ObjectId,
      requied: true,
      ref: 'User',
    },
    // 友帮币领取项目
    project: {
      type: String,
      requied: true,
      enum: CoinProject,
    },
    // 友帮币数量
    num: {
      type: Number,
      requied: true,
      default: 0,
    },
    // 生成时间
    createTime: {
      type: Date,
      requied: true,
      default: Date.now,
    },
    // 领取时间
    receiveTime: {
      type: Date,
      requied: true,
    },
    // 过期时间
    expireTime: {
      type: Date,
      requied: true,
    },
    type: {
      type: String,
      requied: true,
      enum: CoinType,
    },
    // 状态
    status: {
      type: Number,
      requied: true,
      enum: [ CoinStatus.waiting, CoinStatus.received, CoinStatus.overdue ],
    },
  });
  schema.plugin(pageFindPlugin, { limit: 10 });
  schema.index({});
  return mongoose.model('youbangcoin', schema, 'youbangcoin');
};
